import numpy as np
from sklearn.decomposition import PCA
import sklearn.datasets as dts
import matplotlib.pyplot as plt

#加载数据
cancer=dts.load_breast_cancer()
x=cancer.data
y=cancer.target
print(x.shape)

#建模
#整数保留维度大小
#保留信息量大小
#方差衡量
model=PCA(0.99)
z=model.fit_transform(x) #训练并降维，z是降维后的数据
print(z.shape)
print('特征向量：',model.components_)
print('特征值：',model.explained_variance_)
print('特征值方差所占比例：',model.explained_variance_ratio_)
#
# #画图
plt.scatter(z[y==0,0],z[y==0,1],c='b')
plt.scatter(z[y==1,0],z[y==1,1],c='r')
plt.show()
#
# #重建
xnew=model.inverse_transform(z)
print(xnew.shape)